#include "gauss_jacobi.h"

#include <limits>
#include <cmath>


namespace t2c2 {

Gauss_Jacobi::Gauss_Jacobi(Matrix* _matrix, const Vector& _b) : matrix(_matrix), b(_b) {
}

Gauss_Jacobi::~Gauss_Jacobi() {
 	/*if(matrix != 0) delete matrix;
	matrix = 0;*/
}

Vector Gauss_Jacobi::solve(const Vector& x) {
	Vector result(b.get_row());						// result vector

	for (unsigned int i=0; i<x.get_row();i++){			// calculate the next value of x vector
		result[i]= ( b[i] - sum(x,i) )/(*matrix)[i][i];
	}
	
	return result;
}

double Gauss_Jacobi::sum(const Vector& x, unsigned int row) {
	double sum=0;
	for (unsigned int i=0; i<x.get_row(); i++){
		if (i!=row)
			sum=sum + (*matrix)[row][i]*x[i];
	}
	return sum;
}

}
